package com.example.test04.controller;

import com.example.test04.entity.Classroom;
import com.example.test04.service.ClassroomService;
import com.example.test04.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("class")
@CrossOrigin(maxAge = 3600)
public class ClassroomController {
    @Autowired
    private ClassroomService classroomService;
    @PostMapping("save")
    public JsonResult save(@RequestBody Classroom classroom) {
        if (classroom.getName().isEmpty()){
            return JsonResult.failed("教室名字不能为空");
        }
        if (classroom.getAddress().isEmpty()){
            return JsonResult.failed("校区地址不能为空");
        }
        if (classroom.getXiaoQu().isEmpty()){
            return JsonResult.failed("校区位置不能为空");
        }
        Classroom res = classroomService.save(classroom);
        if (res == null){
            return JsonResult.failed("添加失败");
        }
        return JsonResult.success("添加成功",null);
    }

    @GetMapping("delete")
    public JsonResult delete(@RequestParam("id") int id) {
        classroomService.delete(id);
        return JsonResult.success("删除成功",null);
    }

    @GetMapping("list")
    public JsonResult list() {
        List<Classroom> classroomList = (List<Classroom>) classroomService.findAll();
        return JsonResult.success("获取成功",null);
    }
}
